package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.demo.common.Result;
import com.example.demo.entity.Item;
import com.example.demo.entity.User;
import com.example.demo.entity.timeTableColumn;
import com.example.demo.mapper.ItemMapper;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.time.Period;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/selectandgrade")
public class ItemController {
    @Resource
    ItemMapper itemMapper;

    @PostMapping("/selectcourse")
    public Result<?> selectCourse(@RequestBody Item newItem){
        QueryWrapper<Item> addWrapper=new QueryWrapper<Item>();
        addWrapper.eq("student",newItem.getStudent());
        addWrapper.eq("teacher",newItem.getTeacher());
        addWrapper.eq("course",newItem.getCourse());
        Item res = itemMapper.selectOne(addWrapper);
        if (res != null) {
            return Result.error("-1", "重复选课！");
        }
            itemMapper.insert(newItem);
            return Result.success();
    }



    @PostMapping("/dropcourse")
    public Result<?> dropCourse(@RequestBody Item targetItem){
        QueryWrapper<Item> dropWrapper=new QueryWrapper<Item>();
        dropWrapper.eq("student",targetItem.getStudent());
        dropWrapper.eq("teacher",targetItem.getTeacher());
        dropWrapper.eq("course",targetItem.getCourse());
        Item res = itemMapper.selectOne(dropWrapper);
        if (res == null) {
            return Result.error("-1", "尚未选课！");
        }
        itemMapper.delete(dropWrapper);
        return Result.success();
    }

    @PostMapping("/updateitem")
    public Result<?> updateItem(@RequestBody Item newItem){
        itemMapper.updateById(newItem);
        return Result.success();
    }

    @PostMapping("/stuquery")
    public Result<?> stuQueryItem(@RequestBody User user){
        QueryWrapper<Item> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("student",user.getName());
        List<Item> resList=itemMapper.selectList(queryWrapper);
        return Result.success(resList);
    }

    @PostMapping("/teaquery")
    public Result<?> teaQueryItem(@RequestBody User user){
        QueryWrapper<Item> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("teacher",user.getName());
        List<Item> resList=itemMapper.selectList(queryWrapper);
        return Result.success(resList);
    }

    @PostMapping("/namequery")
    public Result<?> nameQueryItem(@RequestBody String courseName){
        QueryWrapper<Item> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("course",courseName);
        List<Item> resList=itemMapper.selectList(queryWrapper);
        return Result.success(resList);
    }

    @PostMapping("/namequeryplusperiod")
    public Result<?> nameQueryPlusPeriod(@RequestBody Item item){
        String courseName=item.getCourse();
        String period=item.getPeriod();
        QueryWrapper<Item> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("course",courseName);
        queryWrapper.eq("period",period);
        List<Item> resList=itemMapper.selectList(queryWrapper);
        return Result.success(resList);
    }

    @PostMapping("/namequeryplusclassid")
    public Result<?> nameQueryPlusClassid(@RequestBody Item item){
        String courseName=item.getCourse();
        String classid=item.getClassid();
        QueryWrapper<Item> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("course",courseName);
        queryWrapper.eq("classid",classid);
        List<Item> resList=itemMapper.selectList(queryWrapper);
        return Result.success(resList);
    }

    @PostMapping("rank")
    public Result<?> rankQuery(@RequestBody String courseName){
        QueryWrapper<Item> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("course",courseName);
        List<Item> resList=itemMapper.selectList(queryWrapper);
        int []number ={0,0,0,0,0};
        int k=0;
        for (Item item : resList) {
            switch (Integer.parseInt(item.getGrade()) / 10) {
                case 0, 1, 2, 3, 4, 5 -> number[0]++;
                case 6 -> number[1]++;
                case 7 -> number[2]++;
                case 8 -> number[3]++;
                default -> number[4]++;
            }
            }
        return Result.success(number);
        }
    @PostMapping("/timeTablequery")
    public Result<?> timeTableQuery(@RequestBody User user){
        QueryWrapper<Item> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("student",user.getName());
        List<Item> resList=itemMapper.selectList(queryWrapper);
        timeTableColumn[] table=new timeTableColumn[6];
        for(int i=0;i<6;i++){
            table[i]=new timeTableColumn();
            table[i].setMonday("");
            table[i].setTuesday("");
            table[i].setWednesday("");
            table[i].setThursday("");
            table[i].setFriday("");
            table[i].setSaturday("");
            table[i].setSunday("");
            table[i].setPosition("第"+(i+1)+"节");
        }
        for (Item curr : resList) {
            String timecollect=curr.getTime();
            int num=timecollect.length()/4;
            for(int j=0;j<=num;j++){
                int whichday=timecollect.charAt(4*j)-48;
                int whichturn=timecollect.charAt(4*j+2)-48;
                switch(whichday){
                    case 1-> table[whichturn-1].setMonday(curr.getCourse());
                    case 2-> table[whichturn-1].setTuesday(curr.getCourse());
                    case 3-> table[whichturn-1].setWednesday(curr.getCourse());
                    case 4-> table[whichturn-1].setThursday(curr.getCourse());
                    case 5-> table[whichturn-1].setFriday(curr.getCourse());
                    case 6-> table[whichturn-1].setSaturday(curr.getCourse());
                    case 7-> table[whichturn-1].setSunday(curr.getCourse());
                }
            }

        }
        return Result.success(table);
    }
    }



